package com.example.demo.outlook;


import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
import com.microsoft.graph.models.Attachment;
import com.microsoft.graph.models.FileAttachment;
import com.microsoft.graph.models.Message;
import com.microsoft.graph.requests.AttachmentCollectionPage;
import com.microsoft.graph.requests.MessageCollectionPage;
import com.microsoft.graph.requests.GraphServiceClient;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
public class OutlookAttachmentDownloader {

    public static void main(String[] args) {
        // 启用详细日志
        System.setProperty("azure.identity.logging.level", "verbose");
        System.setProperty("org.slf4j.simpleLogger.log.com.microsoft.aad.msal4j", "debug");

        final String clientId = "6169a47e-3a2e-4c47-87ac-d91acc524a20";
        final String tenantId = "aaf1697c-4473-4326-bd03-7d25effca28b";
//        final String clientSecret = "0861246b-17a2-4cc0-9fcf-8ed6df7ec07c";
        final String clientSecret = "WOP8Q~noPAxMZBAek.nI4_kuLVWUA_h93Eh0nbNw";
        final String userEmail = "caad@chint.com"; // 替换为实际的用户邮箱 hbxun@chint.com、yangmk@chint.com

        // 1. 使用客户端凭据创建认证对象
        ClientSecretCredential credential = new ClientSecretCredentialBuilder()
                .clientId(clientId)
                .clientSecret(clientSecret)
                .tenantId(tenantId)
                .build();

        // 2. 创建Graph客户端
        TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(
                List.of("https://graph.microsoft.com/.default"), credential);
        GraphServiceClient<Request> graphClient = GraphServiceClient.builder()
                .authenticationProvider(authProvider)
                .buildClient();

        // 3. 获取带附件的邮件
        MessageCollectionPage messages = graphClient.users(userEmail)
                .messages()
                .buildRequest()
                .select("id,subject,hasAttachments")
                .filter("hasAttachments eq true")
                .top(10) // 获取前10封带附件的邮件
                .get();

        // 4. 处理每封邮件，下载附件
        for (Message message : messages.getCurrentPage()) {
            System.out.println("Processing message: " + message.subject);

            // 获取该邮件的所有附件
            AttachmentCollectionPage attachments = graphClient.users(userEmail)
                    .messages(message.id)
                    .attachments()
                    .buildRequest()
                    .get();

            for (Attachment attachment : attachments.getCurrentPage()) {
                if (attachment instanceof FileAttachment) { // 仅处理文件附件
                    FileAttachment fileAttachment = (FileAttachment) attachment;
                    try {
                        // 下载附件
//                        downloadAttachment(graphClient, userEmail, message.id, fileAttachment);
                    } catch (Exception e) {
                        System.err.println("Error downloading attachment: " + e.getMessage());
                    }
                }
            }
        }
    }
}
